﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BrokerPresentation
{



/// <summary>
/// 
/// </summary>
/// <param name="arguments"></param>
/// <returns></returns>
public delegate Object FunctionInvokation(Object arguments);



/// <summary>
/// 
/// </summary>
public class ServerProxy : ServiceInterface
{
    private FunctionInvokation functionInvokation;
    private Broker broker;
    


    /// <summary>
    /// 
    /// </summary>
    /// <param name="aFunctionInvokation"></param>
    public ServerProxy(FunctionInvokation aFunctionInvokation)
    {
        functionInvokation  = aFunctionInvokation;
        broker              = new Broker();

        //Wait for request.
        Object serializedRequest = new Object();

        Object request              = UnmarshalRequest(serializedRequest);
        Object response             = InvokeFunctionA(request);
        Object serializedResponse   = MarshalResponse(response);

        SendResponse(serializedResponse);
    }



    /// <summary>
    /// 
    /// </summary>
    /// <param name="arguments"></param>
    /// <returns></returns>
    public Object InvokeFunctionA(Object arguments)
    {
        return functionInvokation.Invoke(arguments);
    }



    /// <summary>
    /// 
    /// </summary>
    /// <param name="serializedRequest"></param>
    /// <returns></returns>
    private Object UnmarshalRequest(Object serializedRequest)
    {
        //Deserialize the request.
        Object request = serializedRequest;

        return request;
    }



    /// <summary>
    /// 
    /// </summary>
    /// <param name="serializedRequest"></param>
    private void SendResponse(Object serializedRequest)
    {
        broker.forwardResponse(serializedRequest);
    }



    /// <summary>
    /// 
    /// </summary>
    /// <param name="response"></param>
    /// <returns></returns>
    private Object MarshalResponse(Object response)
    {
        //Do something with (e.g. Serialize) the request here.
        Object serializedResponse = response;

        return serializedResponse;
    }
}

}